package cn.sm1234.controller;

import cn.sm1234.service.CustomerService;
import com.example.pojo.Customer;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author http://www.sm1234.cn
 * @version 1.0
 * @description cn.sm1234.controller
 * @date 18/4/1
 */
@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;


    /**
     * 这个不是跳转页面,而是直接返回数据
     */
    @RequestMapping("/query")
    @ResponseBody
    public Customer query(@RequestParam("id") String id) {
        System.out.println("CustomerController中的query方法执行中----id="+id);
        Customer customer = customerService.mySelectByID(id);

        if (customer != null) {
            return customer;
        }else {
            Customer customer1 = new Customer();
            customer1.setName("未找到该用户");
            return customer1;
        }
    }

    @RequestMapping("/query2")
    @ResponseBody
    public Customer query2(@RequestParam("id") String id, @RequestParam("name") String name) {
        System.out.println("CustomerController中的query2方法执行中----id=" + id+",name="+name);
        Customer customer = customerService.mySelectByIDAndName(id,name);

        if (customer != null) {
            return customer;
        } else {
            Customer customer1 = new Customer();
            customer1.setName("未找到该用户");
            return customer1;
        }
    }

    @RequestMapping(value = "/mySelectByPojo", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Customer queryPojo( @RequestBody Customer customer  ) {

        System.out.println("CustomerController中的mySelectByPojo方法执行中----"+customer);

        Customer customer1 = customerService.mySelectByPojo(customer);

        if (customer1 != null) {
            return customer1;
        } else {
            Customer customer2 = new Customer();
            customer2.setName("未找到该用户");
            return customer2 ;
        }
    }

    /**
     * mySelectByPojo2方法会报错,因为xml中的#{_parameter.name}没有写在动态sql标签内.没有经过ognl引擎处理
     * 所以它会报错.
     */
    @RequestMapping(value = "/mySelectByPojo2", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Customer queryPojo2( @RequestBody Customer customer ) {

        System.out.println("CustomerController中的mySelectByPojo2方法执行中----"+customer);

        Customer customer1 = customerService.mySelectByPojo2(customer);

        if (customer1 != null) {
            return customer1;
        } else {
            Customer customer2 = new Customer();
            customer2.setName("未找到该用户");
            return customer2 ;
        }
    }


    /**
     * 跳转到input.jsp
     */
    @RequestMapping("/input")
    public String input() {
        System.out.println("CustomerController中的input方法执行中");
        return "input";
    }


    @PostMapping(value = "/myInsert", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String myInsert(@RequestBody Customer customer) {
        System.out.println("CustomerController中的myInsert方法执行了----" + customer);
        customerService.myInsert(customer);

        return "{\"msg\":\"成功了\"}";
    }


    @RequestMapping(value = "/queryByExample",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String queryByExample(@RequestBody  Customer customer) {

        System.out.println("CustomerController中的queryByExample方法执行了----" + customer);
        customerService.queryByExample(customer);

        return "{\"msg\":\"成功了\"}";
    }

    @RequestMapping(value = "/queryByCondition",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String queryByCondition(@RequestBody  Customer customer) {

        System.out.println("CustomerController中的queryByCondition方法执行了----" + customer);
        customerService.queryByCondition(customer);

        return "{\"msg\":\"queryByCondition----成功了\"}";
    }


    @RequestMapping(value = "/queryByName",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Customer queryByName(@RequestParam("name") String name) {

        System.out.println("CustomerController中的queryByName方法执行了----" + name);

        Customer customer = customerService.queryByName(name);

        return customer;
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    public String save(Customer customer) {
        System.out.println("CustomerController中的customer.getName()=" + customer.getName());
        try {
            customerService.save(customer);
            System.out.println("==========================================================");

            List<Customer> customers = customerService.selectByID("49");
            for (Customer cust : customers) {
                System.out.println("CustomerController中的name=" + cust.getName() + ",age=" + cust.getAge());
                System.out.println("CustomerController中打印Customer对象:" + cust);
            }
            System.out.println("==========================================================");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "succ";
    }
}
